package com.fengmoxi.ctv.activity.config.lamp.pq;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;

import androidx.appcompat.app.AppCompatActivity;

import com.fengmoxi.ctv.R;
import com.fengmoxi.ctv.activity.config.lamp.ConfigLampActivity;
import com.fengmoxi.ctv.manage.HomeButtonReceiver;
import com.fengmoxi.ctv.manage.LampManage;
import com.litao.slider.NiftySlider;

public class ConfigLampBrightnessActivity extends AppCompatActivity {
    private static final String TAG = ConfigLampBrightnessActivity.class.getSimpleName();
    private NiftySlider mSlider;
    private int brightness;
    private LampManage mLampManage;
    private HomeButtonReceiver mHomeButtonReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_config_lamp_brightness);

        mSlider = findViewById(R.id.seekBar);
        mLampManage = new LampManage();

        mSlider.addOnValueChangeListener((slider, value, fromUser) -> {
            int data = Math.round(value);
            if (brightness != data) {
                brightness = data;
                Log.d(TAG, "Brightness Change to: " + brightness);
                mLampManage.setBrightness(brightness);
                refresh();
            }
            return null;
        });

        refresh();

        mHomeButtonReceiver = new HomeButtonReceiver(this, new HomeButtonReceiver.OnHomeBtnPressListener() {
            @Override
            public void onHomeBtnPress() {
                if (ConfigLampActivity.configLampActivity != null) {
                    ConfigLampActivity.configLampActivity.finish();
                }
                finish();
            }

            @Override
            public void onHomeBtnLongPress() {

            }
        });
        mHomeButtonReceiver.start();
    }

    @Override
    protected void onDestroy() {
        // 保存设置
        mLampManage.savePQUserParam();
        mHomeButtonReceiver.stop();
        super.onDestroy();
    }

    public boolean onKeyDown(int kCode, KeyEvent kEvent) {
        switch (kCode) {
            case KeyEvent.KEYCODE_DPAD_LEFT:
            case KeyEvent.KEYCODE_DPAD_DOWN:
                minus();
                return true;
            case KeyEvent.KEYCODE_DPAD_UP:
            case KeyEvent.KEYCODE_DPAD_RIGHT:
                add();
                return true;
        }
        return super.onKeyDown(kCode, kEvent);
    }

    private void refresh() {
        int data = new LampManage().getBrightness();
        Log.d(TAG, "Brightness: " + data);
        if (data >= -31 && data <= 31)
            brightness = data;
        else
            brightness = 0;
        mSlider.setValue(brightness, false);
    }

    private void add() {
        Log.d(TAG, "Brightness add");
        if (brightness < 31) {
            brightness += 3;
            mSlider.setValue(brightness, true);
        }
    }

    private void minus() {
        Log.d(TAG, "Brightness minus");
        if (brightness > -31) {
            brightness -= 3;
            mSlider.setValue(brightness, true);
        }
    }
}